package com.movieproject.testing;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.hibernate.search.query.dsl.QueryBuilder;

import com.movieproject.dataobjects.Movie;

public class SearchTest
	{

	public static void main(String[] args)
		{
		SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
		Session session = sessionFactory.openSession();
		FullTextSession fullTextSession = Search.getFullTextSession(session);
		Transaction tx = fullTextSession.beginTransaction();
		QueryBuilder qb = fullTextSession.getSearchFactory()
			    .buildQueryBuilder().forEntity(Movie.class).get();
		org.apache.lucene.search.Query query = qb
				  .keyword()
				  .onFields("actors", "description", "genre", "title")
				  .matching("action")
				  .createQuery();
		
		org.hibernate.Query hibQuery = 
			    fullTextSession.createFullTextQuery(query, Movie.class);
		
		List<?> l = hibQuery.list();
		for(Object o : l)
			{
			Movie m = (Movie) o;
			System.out.println(m.getTitle());
			}
		}

	}
